package com.njit.office.service.impl;

import com.njit.office.dao.InformMapper;
import com.njit.office.dao.TeamMapper;
import com.njit.office.dao.UserInformsMapper;
import com.njit.office.dataobject.*;
import com.njit.office.form.AddInformForm;
import com.njit.office.model.InformModel;
import com.njit.office.model.InformState;
import com.njit.office.model.Result;
import com.njit.office.service.InformService;
import com.njit.office.service.TeamService;
import com.njit.office.utils.IDUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class InformServiceImpl implements InformService {

    @Resource
    InformMapper informMapper;
    @Resource
    TeamMapper teamMapper;
    @Resource
    TeamService teamService;

    @Resource
    UserInformsMapper userInformsMapper;

    @Override
    public Result createInform(AddInformForm form) {

        Result result = new Result();

        String informId = IDUtils.toUUID();
        Inform inform = new Inform();
        inform.setId(informId);
        inform.setContent(form.getContent());
        inform.setTitle(form.getTitle());
        inform.setTeamId(form.getTeamId());
        inform.setGmtCreated(new Date());

        //插入inform
        informMapper.insert(inform);

        //为组内的所有成员添加通知，便于修改每一个通知状态
        Result res1 = teamService.getAllMembers(form.getTeamId());
        if (res1.isSuccess()) {
            List<LoginUser> userTeams = (List<LoginUser>) res1.getData();
            for (LoginUser loginUser : userTeams) {
                UserInforms userInforms = new UserInforms();
                userInforms.setId(IDUtils.toUUID());
                userInforms.setInformId(informId);
                userInforms.setUserId(loginUser.getUserId());
                userInforms.setState(InformState.UNREAD.name());
                userInformsMapper.insert(userInforms);
            }
            result.setSuccess(true);
        }else{
            result.setSuccess(false);
            result.setErrMsg("获取小组成员失败！");
        }
        return result;
    }

    @Override
    public Result getUnReadInform(String userId) {

        Result result = new Result();

        List<UserInforms> list = userInformsMapper.selectByUserId(userId);

        List<InformModel> resultList = new ArrayList<>();
        for (UserInforms userInforms : list) {

            if (userInforms.getState().equals(InformState.UNREAD.name())) {
                String informId = userInforms.getInformId();
                Inform inform = informMapper.selectByPrimaryKey(informId);
                InformModel informModel = new InformModel();
                informModel.setInformId(inform.getId());
                informModel.setUserInformId(userInforms.getId());
                informModel.setContent(inform.getContent());
                informModel.setTitle(inform.getTitle());
                informModel.setCreateTime(inform.getGmtCreated());
                Team team = teamMapper.selectByPrimaryKey(inform.getTeamId());
                informModel.setTeam(team);
                resultList.add(informModel);
            }
        }

        result.setSuccess(true);
        result.setData(resultList);
        return result;
    }

    @Override
    public Result getUnDoInform(String userId) {
        Result result = new Result();

        List<UserInforms> list = userInformsMapper.selectByUserId(userId);

        List<InformModel> resultList = new ArrayList<>();
        for (UserInforms userInforms : list) {

            if (userInforms.getState().equals(InformState.UNDO.name())) {
                String informId = userInforms.getInformId();
                Inform inform = informMapper.selectByPrimaryKey(informId);
                InformModel informModel = new InformModel();
                informModel.setInformId(inform.getId());
                informModel.setUserInformId(userInforms.getId());
                informModel.setContent(inform.getContent());
                informModel.setCreateTime(inform.getGmtCreated());
                informModel.setTitle(inform.getTitle());
                Team team = teamMapper.selectByPrimaryKey(inform.getTeamId());
                informModel.setTeam(team);
                resultList.add(informModel);
            }
        }


        result.setSuccess(true);
        result.setData(resultList);
        return result;
    }

    @Override
    public Result getFinishedInform(String userId) {
        Result result = new Result();

        List<UserInforms> list = userInformsMapper.selectByUserId(userId);

        List<InformModel> resultList = new ArrayList<>();
        for (UserInforms userInforms : list) {

            if (userInforms.getState().equals(InformState.FINISHED.name())) {
                String informId = userInforms.getInformId();
                Inform inform = informMapper.selectByPrimaryKey(informId);
                InformModel informModel = new InformModel();
                informModel.setInformId(inform.getId());
                informModel.setUserInformId(userInforms.getId());
                informModel.setContent(inform.getContent());
                informModel.setTitle(inform.getTitle());
                informModel.setCreateTime(inform.getGmtCreated());
                Team team = teamMapper.selectByPrimaryKey(inform.getTeamId());
                informModel.setTeam(team);
                resultList.add(informModel);
            }
        }


        result.setSuccess(true);
        result.setData(resultList);
        return result;
    }

    @Override
    public Result updateUserInformsState(String userInformsId, InformState state) {
        Result result = new Result();
        UserInforms userInforms = new UserInforms();
        userInforms.setId(userInformsId);
        userInforms.setState(state.name());
        userInformsMapper.updateByPrimaryKey(userInforms);
        result.setSuccess(true);
        return result;
    }

    @Override
    public Result del(String userInformsId) {
        Result result = new Result();
        userInformsMapper.deleteByPrimaryKey(userInformsId);
        result.setSuccess(true);
        return result;
    }
}
